//Functions for create and value the player hand

int new_card(int card_number) //draw a new card
{
		if(card_number==5)
			{
			ins=1;
			}
	else
	{
	newcard=last_name[card];//Current game is saved in this array

	card_display[((card_number+1)*2)-1]=newcard;
	card_display[((card_number+1)*2)]=last_type[card];
	card++;
	card_played--;
	card_number++;
	}
return card_number;
}


int value_card(unsigned char n_no) //gets the value for an especific card
{
	unsigned char next_value=0;
	int result=0;
	next_value=n_no;

		if(next_value=='A')
			result=1;
		if(next_value=='2')
			result=2;
		if(next_value=='3')
			result=3;
		if(next_value=='4')
			result=4;
		if(next_value=='5')
			result=5;
		if(next_value=='6')
			result=6;
		if(next_value=='7')
			result=7;
		if(next_value=='8')
			result=8;
		if(next_value=='9')
			result=9;
		if(next_value=='0')
			result=10;
		if(next_value=='J')
			result=10;
		if(next_value=='Q')
			result=10;
		if(next_value=='K')
			result=10;
	return result;
}



int calculate_total_player()	// calculate the total value of a hand
	{
	int total_hand=0;
	int aces=0;
	for (i=1;i<11;i=i+2)
		{
		value=value_card(card_display[i]);
		if (value==1)
			aces++;
		total_hand=total_hand+value;
		}
	if (aces>0)
		{
		for (i=0;i<aces;i++)
			{
			if ((10+total_hand)<22)
				total_hand=total_hand+10;
			}
		}
	return total_hand;
	}



void player_hand()  
{
	j=0;
	card_number_player=2;
	for (i=0;i<11;i++)
	{
	card_display[i]='y';
	}
	for(i=0;i<2;i++)
		{
			newcard=last_name[card];// card is the potition and newcard is the next card
			//game_player[j]=newcard;	// cards that has been played			
			//j++;				//count array game

		
			card_display[((i+1)*2)-1]=last_name[card];
			card_display[((i+1)*2)]=last_type[card];
			card++;              // main count
			card_played--;
		}
	total_player=calculate_total_player();	

	card_display[0]='P';
	screen();
	WAIT_SEC(1)
	do
		{
		if(total_player<22)
			{
			notA=~ORDEN;
			if (notA & 0b00100000)
				{
				ins=1;
				}
			if (notA & 0b10000000)
				{
				card_number_player=new_card(card_number_player);
				total_player=calculate_total_player();
				screen();
				WAIT_SEC(1)
				}
			}
		else
			{
			notA=~ORDEN;
			if ((notA & 0b10000000)||(notA & 0b00100000))
				{
				ins=1;
				}
			}
		}
	while(ins==0);
	ins=0;
}
